import { createRouter, createWebHistory, Router, RouteRecordRaw } from 'vue-router';
import { setCurrentPath } from '/@/enum/pageEnum.ts';
import { getAuthCache } from '/@/utils/auth.ts';

const routes: Array<RouteRecordRaw> = [
    {
        path: '/',
        name: 'default',
        redirect: '/login',
    },
    {
        path: '/login',
        name: 'login',
        meta: {
            //西安大屏登录
            title: '登录',
        },
        component: () => import('/@/views/sys/login.vue'),
    },
    {
        path: '/screenLogin',
        name: 'screenLogin',
        meta: {
            //银川大屏登录
            title: '登录',
        },
        component: () => import('/@/views/sys/login.vue'),
    },
    {
        path: '/centralScreenLogin',
        name: 'centralScreenLogin',
        meta: {
            //分中心大屏登录
            title: '登录',
        },
        component: () => import('/@/views/sys/login.vue'),
    },
    {
        path: '/home',
        name: 'index',
        meta: {
            title: '首页',
        },
        component: () => import('/@/views/index.vue'),
    },
    {
        path: '/yinChuanHome',
        name: 'yinChuanHome',
        meta: {
            title: '银川片应急就医',
        },
        component: () => import('/@/views/yinChuan/yinChuan.vue'),
    },
    {
        path: '/yinChuanTest',
        name: 'yinChuanTest',
        meta: {
            title: '银川片应急就医',
        },
        component: () => import('/@/views/yinChuanTest/yinChuanTest.vue'),
    },
    {
        path: '/secondScreen',
        name: 'secondScreen',
        meta: {
            title: '副屏',
        },
        component: () => import('/@/views/indexSon.vue'),
    },
    {
        path: '/transfer',
        name: 'transfer',
        meta: {
            title: '应急大屏',
        },
        component: () => import('/@/views/transfer.vue'),
    },
    {
        path: '/centralScreen',
        name: 'centralScreen',
        meta: {
            title: '应急中心',
        },
        component: () => import('/@/views/centralScreen/centralScreen.vue'),
    },
];

const router: Router = createRouter({
    history: createWebHistory('/'),
    routes: [...routes],
});
const xian = ['/home', '/secondScreen', '/transfer'];
const yinchuan = ['/yinChuanHome', '/yinChuanTest', '/transfer'];

router.beforeEach((to, from, next) => {
    if (to.path !== '/login' && to.path !== '/screenLogin') {
        setCurrentPath(to.path);
    }
    let flag1 = getAuthCache('flag1')?.includes('yinchuan');
    let flag2 = getAuthCache('flag2')?.includes('xian');
    if (to.path == '/yinChuanTest') {
        next();
    }
    if (flag1 && flag2) {
        next();
    } else {
        // 检查是否有权访问目标页面
        if (yinchuan.includes(to.path)) {
            // 如果在 yinchuan 列表中，并且有权限，则导航到目标页面
            if (flag1) {
                next();
            } else {
                next({ path: '/screenLogin' });
            }
        }
        if (xian.includes(to.path)) {
            // 如果在 xian 列表中，并且有权限，则导航到目标页面
            if (flag2) {
                next();
            } else {
                next({ path: '/login' });
            }
        }
        next();
    }
});
export default router;
